package dao;

import java.util.ArrayList;
import java.util.List;

import beans.Servico;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;
import dao.DatabaseHelper;

public class ServicoDAO {
	
	private DatabaseHelper dbHelper;
	private SQLiteDatabase dbWrite;
	private SQLiteDatabase dbRead;
	
	public ServicoDAO(Context context){
		this.dbHelper = new DatabaseHelper(context);
		this.dbWrite = this.dbHelper.getWritableDatabase();
		this.dbRead = this.dbHelper.getReadableDatabase();
	}
	
	public long inserir (Servico servico){
		ContentValues values = new ContentValues();
		values.put(this.dbHelper.getKey_id(), servico.getId());
	    values.put(this.dbHelper.getKey_servico_nome(), servico.getNome());
	    values.put(this.dbHelper.getKey_servico_descricao(), servico.getDescricao());
	    values.put(this.dbHelper.getKey_servico_valor(), servico.getValor());
	    values.put(this.dbHelper.getKey_servico_quantidade_questoes(), servico.getQuantidadeQuestoes());

	 
	    return servico.getId();
	}
	
	public int atualizar (Servico servico){
		ContentValues values = new ContentValues();
		values.put(this.dbHelper.getKey_id(), servico.getId());
	    values.put(this.dbHelper.getKey_servico_nome(), servico.getNome());
	    values.put(this.dbHelper.getKey_servico_descricao(), servico.getDescricao());
	    values.put(this.dbHelper.getKey_servico_valor(), servico.getValor());
	    values.put(this.dbHelper.getKey_servico_quantidade_questoes(), servico.getQuantidadeQuestoes());

	    // update row
	    int retorno = dbWrite.update(this.dbHelper.getTable_servico(), values,
	    		this.dbHelper.getKey_id() + " = ?", 
	    		new String[] { String.valueOf(servico.getId()) });
	 
	    return retorno;
	}
	
	public int deletar (int id){
		return 1;
	}
	
	public Servico obter (int id){
		 
	    String selectQuery = "SELECT  * FROM " + this.dbHelper.getTable_servico() + " WHERE "
	            + this.dbHelper.getKey_id() + " = " + id;
	 
	    Cursor c = dbRead.rawQuery(selectQuery, null);
	 
	    if (c != null){
	        c.moveToFirst();
	    }
	    Servico servico = new Servico();
	    servico.setId(c.getLong(c.getColumnIndex(this.dbHelper.getKey_id())));
	    servico.setDescricao((c.getString(c.getColumnIndex(this.dbHelper.getKey_servico_descricao()))));
	    servico.setValor(c.getDouble(c.getColumnIndex(this.dbHelper.getKey_servico_valor())));
	    servico.setValor(c.getInt(c.getColumnIndex(this.dbHelper.getKey_servico_quantidade_questoes())));
	 
	    return servico;
	}
	
	public List<Servico> listaServicos(){

		List<Servico> servicos = new ArrayList<Servico>();
	    String selectQuery = "SELECT  * FROM " + this.dbHelper.getTable_servico();

	    Cursor c = dbRead.rawQuery(selectQuery, null);
	 
	    // looping through all rows and adding to list
	    if (c.moveToFirst()) {
	        do {

	        	Servico servico = new Servico();
	    	    servico.setId(c.getLong(c.getColumnIndex(this.dbHelper.getKey_id())));
	    	    servico.setDescricao((c.getString(c.getColumnIndex(this.dbHelper.getKey_servico_descricao()))));
	    	    servico.setValor(c.getDouble(c.getColumnIndex(this.dbHelper.getKey_servico_valor())));
	    	    servico.setQuantidadeQuestoes(c.getInt(c.getColumnIndex(this.dbHelper.getKey_servico_quantidade_questoes())));
	    	    servico.setNome((c.getString(c.getColumnIndex(this.dbHelper.getKey_servico_nome()))));
	 

	            // adding to servicos list
	    	    servicos.add(servico);

			} while (c.moveToNext());
		}

	    return servicos;		
	}
	
}
